package com.szs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.szs.common.R;
import com.szs.mapper.EmployeeMapper;
import com.szs.pojo.Employee;
import com.szs.service.EmployeeService;
import com.szs.util.SystemConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;


/**
 * <? extend ...>：上界，只能是当前类或接口的子类（子接口）
 * <? super ...>：下界，当前类向上的所有父类，直到object类
 */
@Slf4j
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
        implements EmployeeService {

    @Override
    public R<Employee> login(HttpServletRequest request, Employee employee) {

        // 对页面输入的密码进行MD5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 根据用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        // 从数据库中取出符合条件的那一条记录
        Employee emp = this.getOne(queryWrapper); // 调用父接口的getOne()方法或者this调用（向上转型）

        // 判断查询结果是否为空
        if (emp == null) {
            return R.error("用户不存在！");
        }

        // 密码比对
        if (!emp.getPassword().equals(password)) {
            return R.error("密码错误！");
        }

        // 判断员工状态
        if (emp.getStatus() == SystemConstant.ACCOUNT_STATUS_OFF) {
            return R.error("账号已禁用！");
        }

        request.getSession().setAttribute("employee", emp.getId());

        return R.success(emp);
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @Override
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @Override
    public R<String> add(HttpServletRequest request, Employee employee) {

        log.info("新增员工信息：{}", employee.toString());

        // 设置用户初始密码为123456
        employee.setPassword(DigestUtils.md5DigestAsHex(SystemConstant.DEFAULT_PASSWORD.getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        // 获取当前用户登录的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        super.save(employee);
        return R.success("新增员工成功");
    }

    @Override
    public R<Page> pageSelf(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {},name={}", page, pageSize, name);

        // 构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        // 构建条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();

        // 添加一个过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        // 添加一个排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        // 执行查询
        super.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    @Override
    public R<String> updateByIdSelf(HttpServletRequest request, Employee employee) {

        log.info(employee.toString());
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        super.updateById(employee);
        return R.success("员工信息更新成功");
    }

    @Override
    public R<Employee> getByIdSelf(long id) {

        log.info("修改的这条员工信息id：{}", id);
        Employee employee = super.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("未找到该员工信息！");
    }

}
